#include "FormHomePage.h"

FormHomePage::FormHomePage(QWidget * parent)
            : BaseFormWidget(parent)
            , m_pFrameMain(nullptr)
            , m_pChatServer(nullptr)
            , m_pAppShow(nullptr)
            , m_bAppShowType(true)
            , m_pAppsForm(nullptr)
            , m_pListForm(nullptr)
{
    // 创建设置窗口
    QFrame *pFrameMain = new QFrame(this);
    pFrameMain->setObjectName("mainFunc");
    addWidget2BaseLayout(pFrameMain);
    //pFrameMain->setStyleSheet(QString("border-image: url(:/homepage/resource/homepage/backimg.jpeg);"));

    m_mapItem2Index.clear();
    m_mapIndex2Info.clear();

    m_pAppLayout = new QVBoxLayout;
    pFrameMain->setLayout(m_pAppLayout);
    m_pAppLayout->setSpacing(1);
    m_pAppLayout->setContentsMargins(5, 0, 5, 5); // 设置上下左右的间距

    m_pFrameMain = new QFrame(pFrameMain);
    m_pFrameMain->setMaximumHeight(50);
    m_pAppLayout->addWidget(m_pFrameMain, Qt::AlignTop);

    // 从本地配置文件中读取帮助信息
    initIconInfos();

    // 创建菜单栏
    newFuncMenu();

    // 创建图标标签页入口
    newAppsForm();

    // 创建列表标签页入口
    newListForm();

    // 默认隐藏列表方式
    if(nullptr != m_pListForm)
    {
        m_pListForm->hide();
    }
}

// 打开设置界面
void FormHomePage::slt_openSetting()
{
    // 每次启用前，更新界面内容
    gSetDialog::Instance().updateParams();
    // 弹出窗口
    gSetDialog::Instance().exec();
    // 获取配置
    if(true == AnaConfig::Instance().getUseChatServer())
    {
        m_pChatServer->show();
    }
    else
    {
        m_pChatServer->hide();
    }
}

// 关闭所有标签页
void FormHomePage::slt_closeAll()
{
    emit sendCloseAll();
}

// 打开帮助界面
void FormHomePage::slt_help()
{
    //myMessageBoxShowOne(m_eProcType, MSG_SHOW_INFO, QString("主页测试消息"));
    gLogShower::Instance().show();
}

// 打开通知界面
void FormHomePage::slt_chatServer()
{
    //gChatServer::Instance().exec();
    gChatServer::Instance().show();
}

// 新建标签页
void FormHomePage::slt_newForm(int iKey)
{
    emit sendNewForm(iKey);
}

// 根据鼠标点击事件获取被点击项，并触发新建标签页
void FormHomePage::slt_itemClicked(QListWidgetItem *item)
{
    if(nullptr == item)
    {
        return;
    }

    auto iter = m_mapItem2Index.find(item);
    if(m_mapItem2Index.end() != iter)
    {
        emit sendNewForm(iter.value());
    }
}

// 切换首页图标展示方式
void FormHomePage::slt_appShow()
{
    m_bAppShowType = !m_bAppShowType;
    if(true == m_bAppShowType)
    {
        // 切换到图标模式
        m_pAppShow->setIcon(QIcon(QString(":/operationIcons/resource/operationIcons/menu_list.ico")));
        m_pAppsForm->show();
        m_pListForm->hide();
    }
    else
    {
        // 切换到列表模式
        m_pAppShow->setIcon(QIcon(QString(":/operationIcons/resource/operationIcons/menu_apps.ico")));
        m_pAppsForm->hide();
        m_pListForm->show();
    }
}

// 创建菜单栏
void FormHomePage::newFuncMenu()
{
    uint iIconSize = 25;
    QMenu *pMoreMenu = new QMenu(m_pFrameMain);
    QAction *m_Setting = new QAction("参数设置");
    QAction *m_CloseAll = new QAction("全部关闭");
    QAction *m_AboutMe = new QAction("关于我");

    m_Setting->setIcon(QIcon(":/operationIcons/resource/operationIcons/setting.ico"));
    m_CloseAll->setIcon(QIcon(":/operationIcons/resource/operationIcons/file_close.ico"));
    m_AboutMe->setIcon(QIcon(":/operationIcons/resource/operationIcons/message_warning.ico"));

    // 菜单指向
    connect(m_Setting, SIGNAL(triggered()), this, SLOT(slt_openSetting()));
    connect(m_CloseAll, SIGNAL(triggered()), this, SLOT(slt_closeAll()));

    pMoreMenu->addAction(m_Setting);
    pMoreMenu->addSeparator();
    pMoreMenu->addAction(m_CloseAll);
    pMoreMenu->addSeparator();
    pMoreMenu->addAction(m_AboutMe);
    pMoreMenu->setFont(gstFont);

    // 设置菜单按钮
    QPushButton *pMenu = new QPushButton(m_pFrameMain);
    pMenu->setMenu(pMoreMenu);
    pMenu->setFlat(true);
    pMenu->setIcon(QIcon(QString(":/operationIcons/resource/operationIcons/option_setting.ico")));
    pMenu->setIconSize(QSize(iIconSize, iIconSize));
    pMenu->setStatusTip(QString("打开设置菜单"));

    // 设置求助按钮
    QPushButton *pHelp = new QPushButton(m_pFrameMain);
    pHelp->setFlat(true);
    pHelp->setIcon(QIcon(QString(":/operationIcons/resource/operationIcons/monito.ico")));
    pHelp->setIconSize(QSize(iIconSize, iIconSize));
    pHelp->setStatusTip(QString("运行日志信息"));
    connect(pHelp, SIGNAL(clicked()), this, SLOT(slt_help()));

    // 设置鱼塘服务端按钮
    m_pChatServer = new QPushButton(m_pFrameMain);
    m_pChatServer->setFlat(true);
    m_pChatServer->setIcon(QIcon(QString(":/operationIcons/resource/operationIcons/fish_forest.ico")));
    m_pChatServer->setIconSize(QSize(iIconSize, iIconSize));
    m_pChatServer->setStatusTip(QString("开启鱼塘服务"));
    connect(m_pChatServer, SIGNAL(clicked()), this, SLOT(slt_chatServer()));

    // 获取配置
    if(true == AnaConfig::Instance().getUseChatServer())
    {
        m_pChatServer->show();
    }
    else
    {
        m_pChatServer->hide();
    }

    m_pAppShow = new QPushButton(m_pFrameMain);
    m_pAppShow->setFlat(true);
    m_pAppShow->setIcon(QIcon(QString(":/operationIcons/resource/operationIcons/menu_list.ico")));
    m_pAppShow->setIconSize(QSize(iIconSize, iIconSize));
    m_pAppShow->setStatusTip(QString("图标展示切换"));
    connect(m_pAppShow, SIGNAL(clicked()), this, SLOT(slt_appShow()));

    QHBoxLayout* pMainFunc = new QHBoxLayout;
    m_pFrameMain->setLayout(pMainFunc);
    pMainFunc->addWidget(pMenu);
    pMainFunc->addWidget(pHelp);
    pMainFunc->addWidget(m_pChatServer);
    pMainFunc->addStretch();
    pMainFunc->addWidget(m_pAppShow);
}

// 创建新建标签入口
void FormHomePage::newAppsForm()
{
    m_pAppsForm = new QWidget(this);
    m_pAppLayout->addWidget(m_pAppsForm);

    QVBoxLayout *pLayout = new QVBoxLayout;
    m_pAppsForm->setLayout(pLayout);

    QHBoxLayout *pLayout1 = new QHBoxLayout;
    QHBoxLayout *pLayout2 = new QHBoxLayout;

    MyPushButton *pBase64 = newFuncWithStyle(QString("Base64"), FORM_BASE64, QString("Base64编解码"), QIcon(QString(":/homepage/resource/homepage/gift_package_party.ico")));
    MyPushButton *pJsonTree = newFuncWithStyle(QString("JSONView"), FORM_JSONTREE, QString("JSON树形图展示和编辑"), QIcon(QString(":/homepage/resource/homepage/storage_files_data.ico")));
    MyPushButton *pChatRoom = newFuncWithStyle(QString("ChatRoom"), FORM_CHATROOM, QString("鱼塘聊天室"), QIcon(QString(":/homepage/resource/homepage/building_business_finance.ico")));
    MyPushButton *pPicRect = newFuncWithStyle(QString("PicRect"), FORM_PICRECT, QString("图片简单画框"), QIcon(QString(":/homepage/resource/homepage/gallery_image_photo.ico")));

    pLayout1->addStretch();
    pLayout1->addWidget(pBase64);
    pLayout1->addStretch();
    pLayout1->addWidget(pJsonTree);
    pLayout1->addStretch();
    pLayout1->setMargin(0);
    pLayout1->setSpacing(0);

    pLayout2->addStretch();
    pLayout2->addWidget(pChatRoom);
    pLayout2->addStretch();
    pLayout2->addWidget(pPicRect);
    pLayout2->addStretch();
    pLayout2->setMargin(0);
    pLayout2->setSpacing(0);

    pLayout->addLayout(pLayout1);
    pLayout->addLayout(pLayout2);
    pLayout->setMargin(100);
    pLayout->setSpacing(20);
    pLayout->addStretch();
}

// 创建列表标签入口
void FormHomePage::newListForm()
{
    m_pListForm = new QWidget(this);
    m_pAppLayout->addWidget(m_pListForm);

    QVBoxLayout *pLayout = new QVBoxLayout;
    m_pListForm->setLayout(pLayout);

    QListWidget* pListWidget = new QListWidget(m_pListForm);
    pListWidget->setIconSize(QSize(50, 50));
    pListWidget->setFrameShape(QListWidget::NoFrame);
    connect(pListWidget, SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(slt_itemClicked(QListWidgetItem*)));
    pLayout->addWidget(pListWidget);

    // 逐行设置按钮内容
    newFunc2ListWidget(pListWidget, QString("Base64"), FORM_BASE64, QIcon(QString(":/homepage/resource/homepage/gift_package_party.ico")));
    newFunc2ListWidget(pListWidget, QString("JSONView"), FORM_JSONTREE, QIcon(QString(":/homepage/resource/homepage/storage_files_data.ico")));
    newFunc2ListWidget(pListWidget, QString("PicRect"), FORM_PICRECT, QIcon(QString(":/homepage/resource/homepage/gallery_image_photo.ico")));
    newFunc2ListWidget(pListWidget, QString("ChatRoom"), FORM_CHATROOM, QIcon(QString(":/homepage/resource/homepage/building_business_finance.ico")));
}

// 添加功能按钮到列表中
void FormHomePage::newFunc2ListWidget(QListWidget* pListWidget, QString strTitle, uint iKey, const QIcon &icon)
{
    if(nullptr == pListWidget)
    {
        return;
    }

    QListWidgetItem *pItem = new QListWidgetItem(pListWidget);
    pItem->setIcon(icon);
    QWidget *pShowItem = new QWidget(pListWidget);
    QHBoxLayout *pItemLayout = new QHBoxLayout;
    pShowItem->setLayout(pItemLayout);

    QLabel* pFuncTitle = new QLabel(pShowItem);     // 设置主题
    QLabel* pFuncInfo = new QLabel(pShowItem);      // 设置使用说明
    pFuncTitle->setText(strTitle);
    pFuncTitle->setMaximumWidth(150);
    pFuncTitle->setFont(QFont("Times New Roman", 18));
    pFuncInfo->setText(QString("未找到有效帮助信息"));
    pFuncInfo->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    pFuncInfo->setFont(QFont("楷体", 12));
    auto iter = m_mapIndex2Info.find(iKey);
    if(m_mapIndex2Info.end() != iter)
    {
        pFuncInfo->setText(iter.value());
    }

    pItemLayout->addWidget(pFuncTitle);
    pItemLayout->addWidget(pFuncInfo);
    pListWidget->setItemWidget(pItem, pShowItem);

    m_mapItem2Index[pItem] = iKey;
}

// 设置按钮风格
MyPushButton* FormHomePage::newFuncWithStyle(QString strTitle, uint iKey, const QString strTip, const QIcon &icon)
{
    MyPushButton * pFunc = new MyPushButton(strTitle, iKey);
    pFunc->setToolTip(strTip);
    pFunc->setStatusTip(strTip);
    pFunc->setFlat(true);
    pFunc->setIcon(icon);

    // 设置大小
    pFunc->setMinimumWidth(200);
    pFunc->setMinimumHeight(80);
    pFunc->setIconSize(QSize(60, 60));
    pFunc->setFont(QFont("Times New Roman", 20));

    connect(pFunc, SIGNAL(SendClicked(int)), this, SLOT(slt_newForm(int)));

    return pFunc;
}

// 从本地默认配置文件中读取帮助信息
void FormHomePage::initIconInfos()
{
    m_mapIndex2Info[FORM_BASE64] = QStringLiteral("支持对本地文件或输入文本的Base64编码，支持对已编码文本进行解码处理；\n支持对已解码后文件（如图片）进行可视化实时展示");
    //m_mapIndex2Info[FORM_JSONSHOW] = QStringLiteral("支持对JSON文本自动格式化处理，关键词上色和展示");
    m_mapIndex2Info[FORM_JSONTREE] = QStringLiteral("按JSON格式规则将数据以树形图形式可视化操作，支持JSON->树形图和树形图->JSON双向转换；\n支持在树形图对JSON数据进行可视化编辑，如对数据的增删改操作");
    //m_mapIndex2Info[FORM_BINARYVIEW] = QStringLiteral("读取本地文件，支持二进制查看方式");
    m_mapIndex2Info[FORM_CHATROOM] = QStringLiteral("支持本地聊天室服务端和客户端使用，聊天内容不本地保存，聊天室人员匿名且自助管理");
    //m_mapIndex2Info[FORM_JSONCMP] = QStringLiteral("根据基准JSON报文，校验另一个报文是否包含或缺失某项关键词");
    m_mapIndex2Info[FORM_PICRECT] = QStringLiteral("支持单个或批量坐标框在指定图片上绘制方框功能，坐标框为归一化坐标；\n支持绘制保存，前后比较等功能，支持批量坐标框报文按需配置解析功能");
}

